package com.jarveis.dbs.core.filter;

import org.apache.log4j.Logger;

import com.jarveis.dbs.Constants;
import com.jarveis.dbs.core.Service;
import com.jarveis.dbs.core.ServiceFactory;
import com.jarveis.frame.util.Param;

/**
 * @desc 参数对象校验
 * @author liuguojun
 * @create 2016-01-10
 */
public class ParamFilter implements Filter {

	private Logger logger = Logger.getLogger(ParamFilter.class);

	public int init() {

		return 0;
	}

	public int filter(Param param) {

		if (param == null) {
			return Constants.ERROR_PARAM;
		}

		String tagName = param.getTagName();
		if (Param.REQ.equals(tagName)) {
			// 验证参数是否正确
			int funcId = param.getHead().getInteger(Param.LABEL_FUNCID);
			if (funcId < 10000 || funcId > 99999) {
				return Constants.ERROR_FUNCID;
			}

			// 验证功能是否存在
			Service service = ServiceFactory.getService(String.valueOf(funcId));
			if (service == null) {
				return Constants.ERROR_SERVICE;
			}

		} else if (Param.RESP.equals(tagName)) {
			int errcode = param.getHead().getInteger(Param.LABEL_ERROR);
			if (errcode > 0 && errcode < 1000 || errcode > 9999) {
				return Constants.ERROR_ERRCODE;
			}
		} else {
			logger.info("参数对象格式异常");
			return Constants.ERROR_PARAM;
		}

		return 0;
	}

	public int destory() {

		return 0;
	}
}
